Sams Teach Yourself Java™ in 24 Hours, Sixth Edition by Rogers Cadenhead
Author:Rogers Cadenhead
Language: eng
Format: epub
ISBN: 9780132841719
Publisher: Sams
Keyboard Events
When a program must react immediately once a key is pressed, it uses keyboard events and the KeyListener interface.
The first step is to register the component that receives key presses by calling its addKeyListener() method. The argument of the method should be the object that implements the KeyListener interface. If it is the current class, use this as the argument.
An object that handles keyboard events must implement three methods:
• void keyPressed(KeyEvent)— A method called the moment a key is pressed
• void keyReleased(KeyEvent)—A method called the moment a key is released
• void keyTyped(KeyEvent)—A method called after a key has been pressed and released
Each of these has a KeyEvent object as an argument, which has methods to call to find out more about the event. Call the getKeyChar() method to find out which key was pressed. This key is returned as a char value, and it only can be used with letters, numbers, and punctuation.
To monitor any key on the keyboard, including Enter, Home, Page Up, and Page Down, you can call getKeyCode() instead. This method returns an integer value representing the key. You then can call getKeyText() with that integer as an argument to receive a String object containing the name of the key (such as Home, F1, and so on).
Listing 15.1 contains a Java application that draws the most recently pressed key in a label by using the getKeyChar() method. The application implements the KeyListener interface, so there are keyTyped(), keyPressed(), and keyReleased() methods in the class. The only one of these that does anything is keyTyped() in Lines 22–25. Create a new Java file called KeyViewer, enter the listing in NetBeans’ source editor, and save the file.
Listing 15.1. The Full Text of KeyViewer.java
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(14616)
The Mikado Method by Ola Ellnestam Daniel Brolund(11877)
Hello! Python by Anthony Briggs(11791)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(11240)
Dependency Injection in .NET by Mark Seemann(11001)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10518)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(9832)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(9420)
Grails in Action by Glen Smith Peter Ledbrook(9165)
Hit Refresh by Satya Nadella(9038)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(8808)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(8594)
The Kubernetes Operator Framework Book by Michael Dame(8470)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8309)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8290)
Robo-Advisor with Python by Aki Ranin(8245)
Practical Computer Architecture with Python and ARM by Alan Clements(8218)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8189)
Building Low Latency Applications with C++ by Sourav Ghosh(8091)